<template>
<view class="help-container">
	<!-- 帮助中心 -->
	<HeaderBar :currentName="_t18(`help_center`)" />
	
	<!-- 搜索框 -->
	<view class="search-section">
		<view class="search-box">
			<view class="search-icon"></view>
			<input 
				v-model="searchKeyword" 
				:placeholder="_t18('search_help')" 
				class="search-input"
				@input="handleSearch"
			/>
		</view>
	</view>
	
	<!-- 帮助内容 -->
	<view v-if="filteredList.length > 0" class="help-content">
		<view class="help-category" v-for="(item, index) in filteredList" :key="index">
			<view class="category-header" @click="toggleCategory(index)">
				<view class="category-title">
					<view class="category-text">{{ item.title }}</view>
				</view>
				<view class="expand-icon" :class="{ 'expanded': expandedCategories.includes(index) }">
					▶
				</view>
			</view>
			
			<view 
				v-if="item.infoList.length > 0" 
				class="question-list"
				:class="{ 'expanded': expandedCategories.includes(index) }"
			>
				<view
					class="question-item"
					v-for="(question, questionIndex) in item.infoList"
					:key="questionIndex"
					@click="$router.push(`/helpDetail/${question.id}`)"
				>
					<view class="question-content">
						<view class="question-text">{{ question.question }}</view>
					</view>
					<view class="arrow-icon">></view>
				</view>
			</view>
		</view>
	</view>
	
	<!-- 无数据状态 -->
	<view v-if="filteredList.length === 0 && isShow" class="no-data-section">
		<view class="no-data-title">{{ _t18('no_help_found') }}</view>
		<view class="no-data-desc">{{ _t18('try_different_keywords') }}</view>
	</view>
	
	<!-- 加载状态 -->
	<view v-if="!isShow" class="loading-section">
		<view class="loading-spinner"></view>
		<view class="loading-text">{{ _t18('loading') }}...</view>
	</view>
</view>
</template>
<script setup>
import { _t18 } from '@/utils/public'
import { getHelpcenter } from '@/api/help/index'
import { onMounted, ref, computed } from 'vue'
import HeaderBar from '@/components/HeaderBar/index.vue'

const list = ref([])
const isShow = ref(false)
const searchKeyword = ref('')
const expandedCategories = ref([])

// 过滤后的列表
const filteredList = computed(() => {
  if (!searchKeyword.value.trim()) {
    return list.value
  }
  
  return list.value.map(category => ({
    ...category,
    infoList: category.infoList.filter(question => 
      question.question.toLowerCase().includes(searchKeyword.value.toLowerCase()) ||
      category.title.toLowerCase().includes(searchKeyword.value.toLowerCase())
    )
  })).filter(category => category.infoList.length > 0)
})

// 搜索处理
const handleSearch = () => {
  // 搜索时自动展开所有分类
  if (searchKeyword.value.trim()) {
    expandedCategories.value = list.value.map((_, index) => index)
  } else {
    expandedCategories.value = []
  }
}

// 切换分类展开状态
const toggleCategory = (index) => {
  const expandedIndex = expandedCategories.value.indexOf(index)
  if (expandedIndex > -1) {
    expandedCategories.value.splice(expandedIndex, 1)
  } else {
    expandedCategories.value.push(index)
  }
}


onMounted(async () => {
  try {
    const res = await getHelpcenter()
    if (res.code === 200) {
      list.value = res.data
      isShow.value = true // 数据加载完成，显示内容
    } else {
      isShow.value = true // 即使没有数据也要显示无数据状态
    }
  } catch (error) {
    console.error('加载帮助数据失败:', error)
    isShow.value = true // 出错时也要显示无数据状态
  }
})
</script>
<style lang="scss" scoped>
.help-container {
  min-height: 100vh;
  // background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

// 搜索区域
.search-section {
  padding: 20px 16px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}

.search-box {
  position: relative;
  background: white;
  border-radius: 25px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  transition: all 0.3s ease;
  
  &:focus-within {
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
    transform: translateY(-2px);
  }
}

.search-icon {
  position: absolute;
  left: 16px;
  top: 50%;
  transform: translateY(-50%);
  width: 18px;
  height: 18px;
  background: #999;
  border-radius: 50%;
  z-index: 2;
  
  &::before {
    content: '';
    position: absolute;
    top: 2px;
    left: 2px;
    width: 8px;
    height: 8px;
    border: 2px solid #999;
    border-radius: 50%;
  }
  
  &::after {
    content: '';
    position: absolute;
    bottom: 2px;
    right: 2px;
    width: 6px;
    height: 2px;
    background: #999;
    transform: rotate(45deg);
  }
}

.search-input {
  width: 100%;
  height: 50px;
  padding: 0 50px 0 50px;
  border: none;
  background: transparent;
  font-size: 16px;
  color: #333;
  
  &::placeholder {
    color: #999;
  }
  
  &:focus {
    outline: none;
  }
}

// 帮助内容
.help-content {
  padding: 0 16px 20px;
}

.help-category {
  margin-bottom: 16px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  transition: all 0.3s ease;
  
  &:hover {
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
    transform: translateY(-2px);
  }
}

.category-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  
  &:hover {
    background: rgba(0, 0, 0, 0.02);
  }
}

.category-title {
  display: flex;
  align-items: center;
  flex: 1;
}

.category-text {
  font-size: 18px;
  font-weight: 600;
  color: #2c3e50;
  line-height: 1.4;
}

.expand-icon {
  font-size: 14px;
  color: #999;
  transition: transform 0.3s ease;
  
  &.expanded {
    transform: rotate(90deg);
  }
}

// 问题列表
.question-list {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease;
  
  &.expanded {
    max-height: 1000px;
  }
}

.question-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-top: 1px solid rgba(0, 0, 0, 0.05);
  cursor: pointer;
  transition: all 0.3s ease;
  
  &:hover {
    background: rgba(0, 0, 0, 0.02);
    transform: translateX(4px);
  }
  
  &:last-child {
    border-bottom: none;
  }
}

.question-content {
  display: flex;
  align-items: center;
  flex: 1;
}

.question-text {
  font-size: 15px;
  color: #34495e;
  line-height: 1.5;
  flex: 1;
}

.arrow-icon {
  font-size: 16px;
  color: #bdc3c7;
  transition: all 0.3s ease;
}

.question-item:hover .arrow-icon {
  color: #3498db;
  transform: translateX(4px);
}

// 无数据状态
.no-data-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  text-align: center;
}

.no-data-title {
  font-size: 20px;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 8px;
}

.no-data-desc {
  font-size: 14px;
  color: #7f8c8d;
  line-height: 1.5;
}

// 加载状态
.loading-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  text-align: center;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid #f3f3f3;
  border-top: 3px solid #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-text {
  font-size: 16px;
  color: #7f8c8d;
}

// 响应式设计
@media (max-width: 480px) {
  .search-section {
    padding: 16px 12px;
  }
  
  .help-content {
    padding: 0 12px 16px;
  }
  
  .category-header {
    padding: 16px;
  }
  
  .category-text {
    font-size: 16px;
  }
  
  .question-item {
    padding: 14px 16px;
  }
  
  .question-text {
    font-size: 14px;
  }
}

// 深色模式支持
@media (prefers-color-scheme: dark) {
  .help-container {
    background: linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%);
  }
  
  .search-section {
    background: rgba(30, 30, 30, 0.9);
    border-bottom-color: rgba(255, 255, 255, 0.1);
  }
  
  .search-box {
    background: #2d2d2d;
    
    &:focus-within {
      box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
    }
  }
  
  .search-input {
    color: #fff;
    
    &::placeholder {
      color: #888;
    }
  }
  
  .help-category {
    background: #2d2d2d;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
  }
  
  .category-text {
    color: #ecf0f1;
  }
  
  .question-text {
    color: #bdc3c7;
  }
  
  .no-data-title {
    color: #ecf0f1;
  }
  
  .no-data-desc {
    color: #95a5a6;
  }
  
  .loading-text {
    color: #95a5a6;
  }
}
</style>
